/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils;

import java.sql.Types;

import net.groupme.exceptions.DeleteFailedException;
import net.groupme.model.Group;
import net.groupme.model.ObjectFactory;
import net.groupme.model.dao.DATABASE_CONSTANTS;

import org.junit.Test;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;


/**
 * created on Jul 28, 2008
 * 
 * This class provides some methods for managing groups/users/etc.
 * in an admanastrative manner...
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.2 $ $Date: 2008-07-28 10:08:43 $
 */
public class ManagementUtility {

	/**
	 * Removes a group (including tags assigned to the group and its resources) from the GroupMe! DB.
	 * <strong>edit groupToRemove variable within this method to select the group to remove </strong>
	 * @throws DeleteFailedException
	 */
	@Test
	public void removeGroup() throws DeleteFailedException {
		//enter ID of group to remove:
		String groupToRemove = "2358"; 
		
		
		SqlUpdate su = new SqlUpdate(ObjectFactory.ds,
				"DELETE FROM " + DATABASE_CONSTANTS.TABLE_USERGROUP + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
					 + DATABASE_CONSTANTS.USER_GROUP_GROUP_ID + " = " + groupToRemove ); //$NON-NLS-1$ //$NON-NLS-2$
		su.compile();
		try {
			su.update();
			} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		 
		su = new SqlUpdate(ObjectFactory.ds,
				"DELETE FROM " + DATABASE_CONSTANTS.TABLE_GROUPRESOURCE + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
					 + DATABASE_CONSTANTS.GROUPRESOURCE_GROUP_ID + " = " + groupToRemove ); //$NON-NLS-1$ //$NON-NLS-2$
		su.compile();
		try {
		
			su.update();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		su = new SqlUpdate(ObjectFactory.ds,
				"DELETE FROM userGroupVisualization WHERE " //$NON-NLS-1$ //$NON-NLS-2$
					 + "groupId = " + groupToRemove ); //$NON-NLS-1$ //$NON-NLS-2$
		su.compile();
		try {
			su.update();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		su = new SqlUpdate(ObjectFactory.ds,
				"DELETE FROM " + DATABASE_CONSTANTS.TABLE_USERTAGGROUPRESOURCE + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
					 + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_GROUPID+ " = " + groupToRemove ); //$NON-NLS-1$ //$NON-NLS-2$
		su.compile();
		try {
			su.update();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		su = new SqlUpdate(ObjectFactory.ds,
				"DELETE FROM " + DATABASE_CONSTANTS.TABLE_USERTAGGROUPRESOURCE + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
					 + DATABASE_CONSTANTS.USERTAGGROUPRESOURCE_RESOURCEID + " = " + groupToRemove ); //$NON-NLS-1$ //$NON-NLS-2$
		su.compile();
		try {
			su.update();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		su = new SqlUpdate(ObjectFactory.ds,
				"DELETE FROM " + DATABASE_CONSTANTS.TABLE_RESOURCE + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
					 + DATABASE_CONSTANTS.RESOURCE_ID + " = " + groupToRemove ); //$NON-NLS-1$ //$NON-NLS-2$
		su.compile();
		try {
			int count = su.update();
			if (count == 0)
				throw new DeleteFailedException();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}
}
